package edu.gatech.cs2340.group7.wheresmystuffapp.views;

//import java.io.InputStream; 
//import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.IReportItemView;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.ItemLocation;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.ReportLostItemPresenter;

/**
 * View for handling UI of reporting a lost item
 * @author tbowling3
 *
 */
public class ReportLostItemView extends IReportItemView implements
		View.OnClickListener, RadioGroup.OnCheckedChangeListener {
	private ReportLostItemPresenter myPresenter;
	private final static int cameraData = 0;
	private String PACKAGE_NAME;
	private EditText etItemName;
	private EditText etDescription;
	private EditText etDateLost;
	private EditText etReward;
	private Button bUploadPic;
	private Button bAddItem;
	private Button bCancel;
	private TextView tvBanner;
	private byte[] picture;
	private Intent cameraIntent;
	private RadioGroup rgCategory;
	
	private Button bCurrentLocation;
	private Button bSelectLocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_report_lost_item);
		linkSetup();

		//InputStream is = getResources().openRawResource(R.drawable.no_photo);
		// picture = BitmapFactory.decodeStream(is);
		PACKAGE_NAME = getApplicationContext().getPackageName();
		myPresenter = new ReportLostItemPresenter(this);
		category = "empty";
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_report_lost_item, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Gets all the data from the Edit Texts and calls AddItem method from the
	 * presenter
	 */
	private void addItem() {
		String itemName = etItemName.getText().toString();
		String description = etDescription.getText().toString();
		String dateLost = etDateLost.getText().toString();
		// String dateLost = etDateLost.getText().toString();
		String reward = etReward.getText().toString();

		myPresenter.addItem(itemName, description, category, location, dateLost, reward, picture);
	} // close addItem

	/**
	 * Method called to allow the user to take a picture Follow NewBoston video
	 * for implementation
	 */
	private void addPic() {
		cameraIntent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(cameraIntent, cameraData);
	}

	/**
	 * Links all the buttons and editTexts to Report Lost Item XML layout
	 */
	private void linkSetup() {
		etItemName = (EditText) findViewById(R.id.et_item_name);
		etItemName.setText(name);
		
		etDescription = (EditText) findViewById(R.id.et_item_description);
		etDescription.setText(description);
		
		etDateLost = (EditText) findViewById(R.id.et_date_lost);
		etReward = (EditText) findViewById(R.id.et_reward);
		bUploadPic = (Button) findViewById(R.id.b_upload_pic);
		bAddItem = (Button) findViewById(R.id.b_add_item);
		bCancel = (Button) findViewById(R.id.b_cancel);
		tvBanner = (TextView) findViewById(R.id.tv_report_lost_banner);
		rgCategory = (RadioGroup) findViewById(R.id.radioItemTypeLost);

		rgCategory.setOnCheckedChangeListener(this);
		bAddItem.setOnClickListener(this);
		bCancel.setOnClickListener(this);
		bUploadPic.setOnClickListener(this);
		
		bCurrentLocation = (Button) findViewById(R.id.b_lost_item_location_current);
		bSelectLocation = (Button) findViewById(R.id.b_lost_item_location_select);
		bCurrentLocation.setOnClickListener(this);
		bSelectLocation.setOnClickListener(this);
	}

	/**
	 * Method called when the user pushes a button on the Report Lost Item
	 * Activity
	 * 
	 * @param view
	 *            The view that was clicked
	 */
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.b_upload_pic:
			addPic();
			// Allows the user to take a picture
			break;
		case R.id.b_add_item:
			addItem();
			break;
		case R.id.b_cancel:
			Intent intent = new Intent(this, UserMainMenuView.class);
			intent.setPackage(PACKAGE_NAME);
			startActivity(intent);
			// Takes user back to Main Menu
			break;
		case R.id.b_lost_item_location_current:
			location = myPresenter.getCurrentLocation();
			break;
		case R.id.b_lost_item_location_select:
			String name = etItemName.getText().toString();
			String description = etDescription.getText().toString();
			String activity = "Lost";
			myPresenter.selectLocation(name, description, category, activity);
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {
			Bundle extras = data.getExtras();
			picture = (byte[]) extras.get("data");
		}
	}

	/**
	 * Sets the text of the result textView to the given string and changes
	 * the color of the textview to red
	 * @param result New text for result textview
	 */
	public void setAddItemResult(String result) {
		tvBanner.setText(result);
		tvBanner.setTextColor(Color.RED);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.radioCategoryKeepsakes:
			category = "Keepsakes";
			break;
		case R.id.radioCategoryHeirlooms:
			category = "Heirlooms";
			break;
		case R.id.radioCategoryMisc:
			category = "Misc";
			break;
		}
	}
	
	public void setLocationLost(ItemLocation l){
		location = l;
	}

}
